/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mr.qa.bo.User;

public class UserSessionUtil 
{
	public final static String SessionName = "user_session";
	
	public final static String VerifyCode = "user_verify_code";
        
        private static final Log log = LogFactory.getLog(UserSessionUtil.class);

	public UserSessionUtil() {
	}

	public static UserSession getUserSession(HttpServletRequest request) {
		UserSession userSession = null;
		HttpSession session = request.getSession(false);
		userSession = getUserSession(session);
		return userSession;
	}

	public static UserSession getUserSession(HttpSession session) {
		UserSession userSession = null;
		if (session != null) {
			userSession = (UserSession) session.getAttribute(SessionName);
		}
		return userSession;
	}

	public static void saveUserSession(UserSession userSession,
			HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		session.setAttribute(SessionName, userSession);
                UserSession us = getUserSession(request);
                //log.info(us.getLoginName());
	}

        public static String getVerificationCodes(HttpServletRequest request) 
        {
                HttpSession session = request.getSession(true);
                return (String)session.getAttribute(VerifyCode);
        }
        
        
        public static void transferUserToUserSession(User user,UserSession userSessoion)
        {
            userSessoion.setUserId(user.getId());
            userSessoion.setLoginName(user.getLoginName());
            userSessoion.setType(user.getType());
            userSessoion.setState(user.getState());
            userSessoion.setName(user.getName());
            userSessoion.setSignature(user.getSignature());
            userSessoion.setEmail(user.getEmail());
            userSessoion.setMobile(user.getMobile());
            userSessoion.setIsSuper(user.getIsSuper());
            userSessoion.setPermissions(user.getPermissions());
            userSessoion.setRegisterDate(user.getRegisterDate());
            userSessoion.setScore(user.getUserScore().getTotalScore());
            userSessoion.setMoney(user.getUserMoney().getTotalMoney());
            userSessoion.setNewMessageSize(user.getNewMessages().size());
            userSessoion.setActive(user.getIsActive());
        }
        
        public static void setAdminPasswordInput(boolean adminPasswordInput, UserSession userSession){
        	userSession.setAdminPasswordInput(true);
        }
}
